package com.jxfgyj.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.jxfgyj.dao.TeacherDao;
import com.jxfgyj.dao.VideoDao;
import com.jxfgyj.domain.DO.jxfgyj_styleEvaluation;
import com.jxfgyj.domain.DO.jxfgyj_teacher;
import com.jxfgyj.domain.DO.jxfgyj_user;
import com.jxfgyj.domain.DO.jxfgyj_video;
import com.jxfgyj.domain.DTO.TeacherStatistics;
import com.jxfgyj.domain.DTO.VideoDTO;
import com.jxfgyj.service.VideoService;
import com.jxfgyj.util.UuidUtil;

public class VideoServiceImpl implements VideoService {

	private VideoDao videoDao;
	private TeacherDao teacherDao;

	public TeacherDao getTeacherDao() {
		return teacherDao;
	}

	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public VideoDao getVideoDao() {
		return videoDao;
	}

	public void setVideoDao(VideoDao videoDao) {
		this.videoDao = videoDao;
	}
	/* 
	 * 
	 */

	@Override
	public void saveVideo(jxfgyj_video video) {
		videoDao.saveVideo(video);
	}

	@Override
	public void evaluationVideo(jxfgyj_styleEvaluation styleEvaluation) {

		styleEvaluation.setStyleEvaluation_id(UuidUtil.getUuid());

		videoDao.evaluationVideo(styleEvaluation);

	}

	@Override
	public List<jxfgyj_video> listVideo(String search) {
		return videoDao.listVideo(search);
	}

	@Override
	public TeacherStatistics getTeacherStatistics(jxfgyj_video video) {
		TeacherStatistics teacherStatistics = new TeacherStatistics();
		// 票数voteNum
		int voteNum = videoDao.getTeacherStatisticsVoteNum(video.getVideo_teacher());
		// 动态教学风格平均分
		double average = videoDao.getTeacherStatisticsAverage(video.getVideo_teacher());
		// 互动票数
		int hudongNum = videoDao.getTeacherStatisticsHudongNum(video.getVideo_teacher());
		// 教授票数
		int jiaoshouNum = videoDao.getTeacherStatisticsJiaoshouNum(video.getVideo_teacher());
		// 混合票数
		int hunheNum = videoDao.getTeacherStatisticsHunheNum(video.getVideo_teacher());
		teacherStatistics.setVoteNum(voteNum);
		teacherStatistics.setAverage(average);
		teacherStatistics.setHudongNum(hudongNum);
		teacherStatistics.setJiaoshouNum(jiaoshouNum);
		teacherStatistics.setHunheNum(hunheNum);
		return teacherStatistics;
	}

	@Override
	public List<VideoDTO> listVideoDTO(jxfgyj_user loginUser, String filter_teacher, String filter_evaluation,
			String search) {
		List<VideoDTO> listVideoDTO = new ArrayList<VideoDTO>();
		List<jxfgyj_video> videoList = videoDao.listVideo(search);
		VideoDTO videoDTO;
		for (jxfgyj_video video : videoList) {
			// 获取所属教师
			jxfgyj_teacher teacher = teacherDao.getTeacher_byID(video.getVideo_teacher());
			/*
			 * 教师筛选
			 */
			if (filter_teacher.equals("-1")) {
				// 不进行筛选
			} else if (!filter_teacher.equals(teacher.getTeacher_id())) {
				continue;
			}

			// 获取这个用户的评价
			jxfgyj_styleEvaluation styleEvaluation = videoDao.getStyleEvaluation_byVideoAndUser(video.getVideo_id(),
					loginUser.getUser_id());
			//
			videoDTO = new VideoDTO();
			videoDTO.setVideo(video);
			videoDTO.setTeacher(teacher);
			videoDTO.setStyleEvaluation(styleEvaluation);
			/*
			 * 筛选评估
			 */
			if (filter_evaluation.equals("1")) {
				// 只要评估过的
				if (styleEvaluation == null) {
					continue;
				} else {
					listVideoDTO.add(videoDTO);
				}
			} else if (filter_evaluation.equals("0")) {
				// 只要没评估过的
				if (styleEvaluation == null) {
					listVideoDTO.add(videoDTO);
				} else {
					continue;
				}
			} else {
				// 都要
				listVideoDTO.add(videoDTO);
			}

		}

		return listVideoDTO;
	}

	@Override
	public void deleteVideo(String video_id) {
		jxfgyj_video video = videoDao.getVideo_byID(video_id);

		/*
		 * 获取路径
		 */
		String lj = "";
		try {
			Properties props = new Properties();
			props.load(this.getClass().getClassLoader().getResourceAsStream("file.properties"));
			lj = props.getProperty("lj");
		} catch (Exception e) {
			e.printStackTrace();
		}

		File root = null;
		File[] allFiles = null;
		// 大图
		root = new File(lj + "video");
		allFiles = root.listFiles();
		for (File file : allFiles) {
			file.getName();
			if (file.getName().equals(video.getVideo_name())) {
				file.delete();
			}
		}
		videoDao.deleteVideo_byID(video_id);
		/*
		 * 一并删除标注
		 */
		deleteEvaluation_byVideo(video_id);
	}

	@Override
	public void deleteEvaluation_byVideo(String styleEvaluation_video) {
		videoDao.deleteEvaluation_byVideo(styleEvaluation_video);

	}

	@Override
	public void deleteVideo_byTeacher(String teacher_id) {
		videoDao.deleteVideo_byTeacher(teacher_id);
		/*
		 * 一并删除标注
		 */
		List<jxfgyj_video> videoList = listVideo_byTeacher(teacher_id);
		for (jxfgyj_video video : videoList) {
			deleteEvaluation_byVideo(video.getVideo_id());
		}

	}

	@Override
	public List<jxfgyj_video> listVideo_byTeacher(String teacher_id) {
		return videoDao.listVideo_byTeacher(teacher_id);
	}

	@Override
	public void deleteEvaluation(String styleEvaluation_id) {
		videoDao.deleteEvaluation(styleEvaluation_id);

	}
}
